/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.beepmystuff;

import android.database.Cursor;
import android.test.AndroidTestCase;

/**
 * Tests the ScanRegistry.
 * 
 * Uses a temporary file to back the ScanRegistry's sqlite database.
 * TODO: more tests!
 */
public class ScanRegistryTest extends AndroidTestCase {
  private ScanRegistry scanRegistry;
  private MockClock mockClock;

  private static class MockClock implements ScanRegistry.Clock {
    public void setTime(long time) {
      now = time;
    }
    
    private long now;

    @Override
    public long now() {
      return now;
    }
  }

  @Override
  public void setUp() {
    mockClock = new MockClock();
    scanRegistry = new ScanRegistry(getContext(), mockClock, null);
  }

  @Override
  public void tearDown() {
    scanRegistry.close();
  }
  
  /**
   * Ensure a fresh database is empty.
   */
  public void testDatabaseInitiallyEmpty() {
    Cursor displayCursor = scanRegistry.getDisplayCursor();
    assertTrue(displayCursor.isBeforeFirst());
    assertTrue(displayCursor.isAfterLast());
  }
  
  /**
   * Add an item and test it is the only item in the database.
   */
  public void testAddItem() {
    long id = scanRegistry.addEan("12345", BmsClient.ShareWith.PUBLIC);
    Cursor displayCursor = scanRegistry.getDisplayCursor();
    assertTrue(displayCursor.isBeforeFirst());
    assertFalse(displayCursor.isAfterLast());
    assertTrue(displayCursor.moveToNext());
    ScanRegistry.Item item = scanRegistry.createItemFromCursor(displayCursor);
    assertEquals(item.getId(), id);
    assertEquals("12345", item.getEan());
    assertFalse(displayCursor.moveToNext());
    assertTrue(displayCursor.isAfterLast());
  }
}
